package cn.dnui_wnx725.dnuifood_16110100725.controller;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import cn.dnui_wnx725.dnuifood_16110100725.R;
import cn.dnui_wnx725.dnuifood_16110100725.bean.RegistBean;
import cn.dnui_wnx725.dnuifood_16110100725.listener.TListener;
import cn.dnui_wnx725.dnuifood_16110100725.model.RegistModel;

public class RegisterActivity extends BaseActivity {
    EditText username,userpass,mobilenum,address,comment,userpass2;
    Button register,btn_back;

    TListener<RegistBean> tListener = new TListener<RegistBean>() {
        @Override
        public void onResponse(RegistBean registBean) {
            String success = registBean.getSuccess();
            if(success.equals("1")){
                showToast("注册成功");
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                intent.putExtra("uname",username.getText().toString());
                setResult(RESULT_OK,intent);
//            startActivityForResult(intent,1);
                finish();
            }else {
                showToast("注册失败");
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    void initView() {
        setLayout_file(R.layout.activity_register);
        //findviewbyid
        username=(EditText)findViewById(R.id.username);
        userpass=(EditText)findViewById(R.id.userpass);
        userpass2=(EditText)findViewById(R.id.userpass2);
        mobilenum=(EditText)findViewById(R.id.mobilenum);
        address=(EditText)findViewById(R.id.address);
        comment=(EditText)findViewById(R.id.comment);
        register=findViewById(R.id.register);
        btn_back=findViewById(R.id.btn_back);
    }

    @Override
    void initEvent() {
        //setonclilistener


        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String uname=username.getText().toString();
                String upw=userpass.getText().toString();
                String upw2=userpass2.getText().toString();
                String unum=mobilenum.getText().toString();
                String uadd = address.getText().toString();
                String ucomment = comment.getText().toString();
                RegistModel registModel = new RegistModel();
                if(!upw.equals(upw2)){
                    showToast("密码不一致!");
//                    showToast(upw2);
                }
                else if(upw.equals("")){
                    showToast("密码为空!");
                }
                else if(uname.equals("")){
                    showToast("用户名为空!");
                }
                else if(unum.equals("")){
                    showToast("手机号为空!");
                }
                else if(uadd.equals("")){
                    showToast("地址为空!");
                }
                else if(ucomment.equals("")){
                    showToast("备注为空!");
                }
                else {
                    registModel.getRegistMassage(uname,upw,unum,uadd,ucomment,tListener);
                }
            }
        });
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    void initData() {

    }
}
